package ex_11_setNewLock;

public class ChangeLock implements Runnable{
    String lock = "123";

    @Override
    public void run() {
        try {
            synchronized (lock){
                System.out.println(Thread.currentThread().getName() + " begin ");
                lock = "456";
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName() + " end ");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * =====================================
     * 更改锁对象,多个线程之间异步执行了
     *
     * =====================================
     */
    public static void main(String[] args) throws Exception{
        ChangeLock changeLock = new ChangeLock();
        Thread t1 = new Thread(changeLock);
        t1.setName("AA");
        Thread t2 = new Thread(changeLock);
        t2.setName("BB");
        t1.start();
        Thread.sleep(20);
        t2.start();
    }
}
